package com.huawei.struts.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.st.model.LjwCatalogT;

/**
 * A data access object (DAO) providing persistence and search support for
 * LjwCatalogT entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.st.model.LjwCatalogT
 * @author MyEclipse Persistence Tools
 */

public class LjwCatalogTDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(LjwCatalogTDAO.class);
	// property constants
	public static final String CATALOG_NAME = "catalogName";
	public static final String CATALOG_ORDER = "catalogOrder";
	public static final String CATALOG_REMARK = "catalogRemark";
	public static final String PARENT_ID = "parentId";
	public static final String CATALOG_URL = "catalogUrl";
	public static final String CATALOG_TYPE = "catalogType";

	protected void initDao() {
		// do nothing
	}

	public void save(LjwCatalogT transientInstance) {
		log.debug("saving LjwCatalogT instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(LjwCatalogT persistentInstance) {
		log.debug("deleting LjwCatalogT instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public LjwCatalogT findById(java.lang.Integer id) {
		log.debug("getting LjwCatalogT instance with id: " + id);
		try {
			LjwCatalogT instance = (LjwCatalogT) getHibernateTemplate().get(
					"com.st.model.LjwCatalogT", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LjwCatalogT instance) {
		log.debug("finding LjwCatalogT instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LjwCatalogT instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from LjwCatalogT as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCatalogName(Object catalogName) {
		return findByProperty(CATALOG_NAME, catalogName);
	}

	public List findByCatalogOrder(Object catalogOrder) {
		return findByProperty(CATALOG_ORDER, catalogOrder);
	}

	public List findByCatalogRemark(Object catalogRemark) {
		return findByProperty(CATALOG_REMARK, catalogRemark);
	}

	public List findByParentId(Object parentId) {
		return findByProperty(PARENT_ID, parentId);
	}

	public List findByCatalogUrl(Object catalogUrl) {
		return findByProperty(CATALOG_URL, catalogUrl);
	}

	public List findByCatalogType(Object catalogType) {
		return findByProperty(CATALOG_TYPE, catalogType);
	}

	public List findAll() {
		log.debug("finding all LjwCatalogT instances");
		try {
			 HibernateTemplate template = this.getHibernateTemplate();    
			 template.setCacheQueries(true);
			 String queryString = "from LjwCatalogT u order by u.catalogOrder";
			return template.find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public LjwCatalogT merge(LjwCatalogT detachedInstance) {
		log.debug("merging LjwCatalogT instance");
		try {
			LjwCatalogT result = (LjwCatalogT) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(LjwCatalogT instance) {
		log.debug("attaching dirty LjwCatalogT instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(LjwCatalogT instance) {
		log.debug("attaching clean LjwCatalogT instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static LjwCatalogTDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (LjwCatalogTDAO) ctx.getBean("LjwCatalogTDAO");
	}
	
	public List showCatalogByParentId(final Integer parentId) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer hql=new StringBuffer();
				hql.append("from LjwCatalogT u where 1=1");
				Query query=queryMap(hql,parentId,session);
				List list = query.list();
				return list;
			}
		});
		return list;
	}
	
	public  Query queryMap(final StringBuffer hql,final Integer parentId,Session session)
	{
		Map map=new HashMap();
		if(parentId!=null && parentId.toString().length()>0)
		{
			map.put("parentId", parentId);
		    hql.append(" and u.parentId =:parentId order by u.catalogOrder asc");

		}
		Query query = session.createQuery(hql.toString());
		
		Iterator it = map.keySet().iterator();

         while (it.hasNext()){

         Object key = it.next(); 

         query.setParameter(key.toString(), map.get(key)); 

         }
         return query;
	}
	
}